<?php
class Course extends CI_Controller 
{
		
    function __construct()
	{
		parent::__construct();	
		$this->load->model('course_model');
		$this->load->model('theme_model');
		$this->load->language('class','english');
	}
	function getPath($action = '')
	{
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index($page = 0)
	{
		$per_page_count 	     = $this->config->item('per_page_count');
		$view 				     = $this->getPath(__FUNCTION__);
		if(isset($_POST['search']))
		{
			$course              = $this->input->post('course');         // filter subject with course 
			$view['filter1']     = $course ;  
			$this->session->set_userdata(array('filter' => $course));    // set filter value
			
		}
		else
		{
			$view['filter1']     = $this->session->userdata('filter');
		}
		if(!empty($view['filter1']))
		{
		$like['name']             = $view['filter1'];   //$course;
		}
		$where                    = array('deleted' => 0);
		$course                   = $this->course_model->getCourses($page, $per_page_count , $like, $where );
		$view['course'] 		  = $course->records;
		$view['cur_page'] 	      = $page;
		$view['title']		      = 'Standards Listing';
		$view['subtitle']	      = 'Standards Lists';
		/************************ Pagination block ***********************************/
		$config['base_url'] 	  = $this->config->item('admin_base_url').'/course/index/';
		$config['total_rows'] 	  = $course->total;
		$config['per_page']       = $per_page_count;
		$config['cur_page']       = $page /$per_page_count;
		$config['uri_segment'] 	  = 4;
		$config['num_links'] 	  = 3;
		$this->pagination->initialize($config);
		$view['msg'] 	          = $this->lang->line($this->session->flashdata('course_msg'));
		$this->layout->adminview('class/course_view', $view);
	
	}
	function add($cur_page = 0)
	{
		$view                    = $this->getPath(__FUNCTION__);
		$view['title']           = "Course Details";
		$save                    = $this->input->post('save');
		$view['course'] 	     = array();
		$view['cur_page']        = $cur_page;
		$view['subtitle']	     = 'Add Course';
		if(!empty($save)) 
		{
			$data                 = $this->retrieve->post($this->course_model->fields);
			$insertStatus         = $this->course_model->add($data);
			$view['course'] 	  = $data;
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('course_msg', 'course_add_success');
					if(!empty($cur_page)) 
						redirect($this->config->item('admin_base').'course/index/'.$cur_page,'location');
					else
						redirect($this->config->item('admin_base').'course/index/','location');
					exit;		
					break;
				case -1:
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('course_valid_failed');
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('course_already_exists');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('course_add_failure');
	
					break;
				
				default:
					$view['msg'] 	= $this->lang->line('course_add_failure');
					break;
			}
		} 
		$this->layout->adminview('class/course_addedit', $view);
	}

	function edit($id = '', $cur_page='')
	{
		$view                 = $this->getPath(__FUNCTION__);
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base').'course/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$records = $this->course_model->getCourses(0,1, array('id' => $id));
			$course = $records->records[0];
			if(empty($course)) 
			{
				$this->session->set_flashdata('course_msg', 'course_invalid_record');
				redirect($this->config->item('admin_base').'course/index','location');
				exit;
			}
			$view['id']		                = $id;
			
			$view['course']	                = $course;
		}
		
		$view['title']                 = "Edit course ";
		$view['page_title']            = "Edit course";
		$view['subtitle']	           = 'Edit course';
		$save                          = $this->input->post('save');
		if(!empty($save)) 
		{
			$data                      = $this->retrieve->post($this->course_model->fields);
			$insertStatus              = $this->course_model->udpate($id, $data);
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('course_msg', 'course_edit_success');
					redirect($this->config->item('admin_base').'course/index/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('course_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('course_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('course_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('course_edit_failure');
					break;
			}
			$view['course'] 	       = $data;
		}
		$view['cur_page']              = $cur_page; 
		$this->layout->adminview('class/course_addedit', $view);
	}
	
	function cancel()
	{
    redirect($this->config->item('admin_base').'course/index','location');
		exit;	
	}
	
	function delete($id = '', $cur_page)
	{
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url').'course/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$deleteStatus = $this->course_model->delete($id);
			switch($deleteStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'course_delete_success');
					redirect($this->config->item('admin_base').'course/index/'.$cur_page,'location');
					exit;		
					break;
				case false:
					$view['msg'] 	    = $this->lang->line('course_delete_failure');
					break;
			}
		}
		
	}
	function clearFilter()
	{
		$this->session->unset_userdata('filter');
		redirect($this->config->item('admin_base').'course/index/','location');
	}
}
?>